﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using GreatB2C.Utility;
using GreatB2C.Business;
using GreatB2C.Config.Shop;

namespace GreatB2C.Web.Manage.SystemSet.Shop
{
    public partial class Package : Inherit.ManagePage
    {
        Business.Package BLL = new Business.Package( );

        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                if ( Request.QueryString[ "Action" ] != null && Request.QueryString[ "Action" ].ToString( ) != "" )
                {
                    if ( Request.QueryString[ "Action" ].ToString( ).Trim( ) == "Modify" )
                    {
                        lblTitle.Text = "修改包装";
                        lblTitle1.Text = "修改包装";
                        BtnSubmit.Text = "修 改[S]";

                        LoadData( Request.QueryString[ "PackageID" ].ToString( ) );
                    }
                    else
                    {
                        lblTitle.Text = "添加包装";
                        lblTitle1.Text = "添加包装";
                        BtnSubmit.Text = "添 加[S]";
                    }
                }
            }
        }

        protected void LoadData( string packageID )
        {
            if ( packageID.Trim( ) != "" )
            {
                Model.Package model = new Model.Package( );
                model.PackageID = Convert.ToInt32( packageID );
                model = BLL.ListByID( model );
                if ( model != null )
                {
                    txtPackageName.Text = model.PackageName;
                    txtPackageWeight.Text = model.PackageWeight.ToString( "F2" );
                    txtGoodsWeightMin.Text = model.GoodsWeightMin.ToString( "F2" );
                    txtGoodsWeightMax.Text = model.GoodsWeightMax.ToString( "F2" );
                }
            }
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            Model.Package model = new GreatB2C.Model.Package( );
            model.PackageID = -1;
            model.PackageName = txtPackageName.Text.Trim( );
            model.PackageWeight = Convert.ToDecimal( txtPackageWeight.Text.Trim( ) );
            model.GoodsWeightMin = Convert.ToDecimal( txtGoodsWeightMin.Text.Trim( ) );
            model.GoodsWeightMax = Convert.ToDecimal( txtGoodsWeightMax.Text.Trim( ) );

            bool isExist = BLL.IsExists( model );
            if ( isExist )
            {
                litScript.Text = "<script type='text/javascript'>ShowError('错误','该包装信息已经存在，请重新输入！');</script>";
                return;
            }
            else
            {
                if ( Request.QueryString[ "Action" ].ToString( ).Trim( ) == "Modify" )
                {
                    model.PackageID = Convert.ToInt32( Request.QueryString[ "PackageID" ].ToString( ) );
                    bool isSuccess = BLL.Update( model );
                    if ( isSuccess )
                    {
                        litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','包装信息修改成功！','PackageManage.aspx');</script>";
                        return;
                    }
                    else
                    {
                        litScript.Text = "<script type='text/javascript'>ShowError('操作失败','包装信息修改失败！');</script>";
                        return;
                    }
                }
                else
                {
                    bool isSuccess = BLL.Insert( model );
                    if ( isSuccess )
                    {
                        litScript.Text = "<script type='text/javascript'>ShowInfoAndRedirect('操作成功','包装信息添加成功！','PackageManage.aspx');</script>";
                        return;
                    }
                    else
                    {
                        litScript.Text = "<script type='text/javascript'>ShowError('操作失败','包装信息添加失败！');</script>";
                        return;
                    }
                }
            }
        }
    }
}
